﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace XiaoMan.Service
{
    public static class MD5Manager
    {       
        /// <summary>
        /// MD5-默认为32位
        /// </summary>
        /// <param name="source">原字符串</param>
        /// <param name="need16">是否输出16位</param>
        /// <param name="toUpper">是否输出大写</param>
        /// <returns></returns>
        public static string Gen_MD5(string source, bool need16 = false, bool toUpper = true)
        {
            var _toUpper = toUpper ? "X2" : "x2";
            if (string.IsNullOrWhiteSpace(source)) { return null; }
            string _md5_code = string.Empty;
            try
            {
                MD5 _md5 = MD5.Create();
                byte[] _t = _md5.ComputeHash(Encoding.UTF8.GetBytes(source));
                for (int i = 0; i < _t.Length; i++)
                {
                    _md5_code += _t[i].ToString(_toUpper);
                }
                if (need16)
                {
                    _md5_code = _md5_code.Substring(8, 16);
                }
                return _md5_code;
            }
            catch { return string.Empty; }
        }     

        //RSA加密,随机生成公私钥对并作为出参返回
        public static string RSA_Encrypt(string str_Plain_Text, out string str_Public_Key, out string str_Private_Key)
        {
            str_Public_Key = "";
            str_Private_Key = "";
            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            byte[] DataToEncrypt = ByteConverter.GetBytes(str_Plain_Text);
            try
            {
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
                str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
                byte[] bytes_Cypher_Text = RSA.Encrypt(DataToEncrypt, false);
                string str_Cypher_Text = Convert.ToBase64String(bytes_Cypher_Text);
                return str_Cypher_Text;
            }
            catch (CryptographicException)
            {
                return null;
            }
        }

        //RSA解密
        public static string RSA_Decrypt(string str_Cypher_Text, string str_Private_Key)
        {
            byte[] DataToDecrypt = Convert.FromBase64String(str_Cypher_Text);
            try
            {
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                byte[] bytes_Public_Key = Convert.FromBase64String(str_Private_Key);
                RSA.ImportCspBlob(bytes_Public_Key);
                byte[] bytes_Plain_Text = RSA.Decrypt(DataToDecrypt, false);
                UnicodeEncoding ByteConverter = new UnicodeEncoding();
                string str_Plain_Text = ByteConverter.GetString(bytes_Plain_Text);
                return str_Plain_Text;
            }
            catch (CryptographicException)
            {
                return null;
            }
        }

        /// <summary>
        /// 获取随机数
        /// </summary>
        public static string GetNonCryptoRandomDataAsBase64(int binaryLength)
        {
            byte[] buffer = new byte[binaryLength];
            new Random().NextBytes(buffer);
            string uniq = Convert.ToBase64String(buffer);
            return uniq;
        }
    }
}
